/*
 * Created on 2004-9-19
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package temp;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class GetMD5 {

	public static void main(String[] args) {
		System.out.println(unicodeMd5("123456"));
	}

	/*
	 * MD5加密函数,函数名:getMD5Mac 输入:字符数组(byte[]) 输出:字符数组(byte[])
	 * 功能:仅对获得的字符数组进行MD5操作,无填充,无MAC密钥 函数头:public byte[] getMD5Mac(byte[]
	 * bySourceByte)
	 */
	public static byte[] getMD5Mac(byte[] bySourceByte) {
		byte[] byDisByte;
		MessageDigest md;

		try {
			md = MessageDigest.getInstance("MD5");
			md.reset();
			md.update(bySourceByte);
			byDisByte = md.digest();
		} catch (NoSuchAlgorithmException n) {
			return (null);
		}
		return (byDisByte);
	}

	/*
	 * MD5加密函数,函数名:getMD5Mac 输入:字符串(String) 输出:字符串(String)
	 * 功能:仅对获得的字符串进行MD5操作,无填充,无MAC密钥 函数头:public String getMD5Mac(String
	 * stSourceString)
	 */

	public static String getMD5Mac(String stSourceString) {
		String mystring;
		byte getbyte[];
		getbyte = getMD5Mac(stSourceString.getBytes());
		mystring = bintoascii(getbyte);
		return (mystring);
	}

	public static String bintoascii(byte[] bySourceByte) {
		int len, i;
		byte tb;
		char high, tmp, low;
		String result = new String();
		len = bySourceByte.length;
		for (i = 0; i < len; i++) {
			tb = bySourceByte[i];

			tmp = (char) ((tb >>> 4) & 0x000f);
			if (tmp >= 10) {
				high = (char) ('a' + tmp - 10);
			} else {
				high = (char) ('0' + tmp);
			}
			result += high;
			tmp = (char) (tb & 0x000f);
			if (tmp >= 10) {
				low = (char) ('a' + tmp - 10);
			} else {
				low = (char) ('0' + tmp);
			}

			result += low;

		}
		return result;
	}

	public static boolean MACCompare(String message, String mac) {
		String mystring;
		mystring = getMD5Mac(message);
		return (mystring.equals(mac));

	}

	private static String HEX = "0123456789abcdef";

	public static String unicodeMd5(String s) {
		int[] md5 = unicodeMd5(s, 0, s == null ? 0 : s.length());
		char[] md5s = new char[32];
		int c = 0;
		for (int i = 0; i < 4; i++) {
			md5s[c++] = HEX.charAt(md5[i] >>> 28);
			md5s[c++] = HEX.charAt((md5[i] >>> 24) & 15);
			md5s[c++] = HEX.charAt((md5[i] >>> 20) & 15);
			md5s[c++] = HEX.charAt((md5[i] >>> 16) & 15);
			md5s[c++] = HEX.charAt((md5[i] >>> 12) & 15);
			md5s[c++] = HEX.charAt((md5[i] >>> 8) & 15);
			md5s[c++] = HEX.charAt((md5[i] >>> 4) & 15);
			md5s[c++] = HEX.charAt(md5[i] & 15);
		}
		return String.valueOf(md5s);
	}

	public static int[] unicodeMd5(String str, int from, int end1) {
		int[] x = new int[20]; // x[16-19] means the md result in little-endian
		x[16] = 0x67452301;
		x[17] = 0xEFCDAB89;
		x[18] = 0x98BADCFE;
		x[19] = 0x10325476;
		int n;
		int d;
		for (n = 0, d = from; d < end1 - 1; d += 2) {
			x[n++] = (str.charAt(d) >>> 8) | ((str.charAt(d) & 0xFF) << 8) //
					| ((str.charAt(d + 1) >>> 8) << 16) | ((str.charAt(d + 1) & 0xFF) << 24);
			if (n == 16) {
				Transform(x);
				n = 0;
			}
		}
		if (d == end1) {
			x[n++] = 0x80;
		} else {
			/* d == end1 - 1 */
			x[n++] = (str.charAt(d) >>> 8) | ((str.charAt(d) & 0xFF) << 8) | 0x800000;
		}
		if (n == 15) {
			x[n++] = 0;
		}
		if (n == 16) {
			Transform(x);
			n = 0;
		}
		if (n < 14) {
			for (; n < 14; n++) {
				x[n] = 0;
			}
		}
		x[14] = (end1 - from) << 4;
		x[15] = (end1 - from) >> 28;
		Transform(x);
		x[0] = x[19];
		x[1] = x[18];
		x[2] = x[17];
		x[3] = x[16];
		return x;
	}

	private static int F(int x, int y, int z) {
		return (x & y) | ((~x) & z);
	}

	private static int G(int x, int y, int z) {
		return (x & z) | (y & (~z));
	}

	private static int H(int x, int y, int z) {
		return x ^ y ^ z;
	}

	private static int I(int x, int y, int z) {
		return y ^ (x | (~z));
	}

	private static int FF(int a, int b, int c, int d, int x, int s, int ac) {
		a += F(b, c, d) + x + ac;
		return ((a << s) | (a >>> (32 - s))) + b;
	}

	private static int GG(int a, int b, int c, int d, int x, int s, int ac) {
		a += G(b, c, d) + x + ac;
		return ((a << s) | (a >>> (32 - s))) + b;
	}

	private static int HH(int a, int b, int c, int d, int x, int s, int ac) {
		a += H(b, c, d) + x + ac;
		return ((a << s) | (a >>> (32 - s))) + b;
	}

	private static int II(int a, int b, int c, int d, int x, int s, int ac) {
		a += I(b, c, d) + x + ac;
		return ((a << s) | (a >>> (32 - s))) + b;
	}

	private static void Transform(int[] x) {
		int a = x[16], b = x[17], c = x[18], d = x[19];
		//
		// Round 1
		a = FF(a, b, c, d, x[0], 7, 0xD76AA478);
		d = FF(d, a, b, c, x[1], 12, 0xE8C7B756);
		c = FF(c, d, a, b, x[2], 17, 0x242070DB);
		b = FF(b, c, d, a, x[3], 22, 0xC1BDCEEE);
		//
		a = FF(a, b, c, d, x[4], 7, 0xF57C0FAF);
		d = FF(d, a, b, c, x[5], 12, 0x4787C62A);
		c = FF(c, d, a, b, x[6], 17, 0xA8304613);
		b = FF(b, c, d, a, x[7], 22, 0xFD469501);
		//
		a = FF(a, b, c, d, x[8], 7, 0x698098D8);
		d = FF(d, a, b, c, x[9], 12, 0x8B44F7AF);
		c = FF(c, d, a, b, x[10], 17, 0xFFFF5BB1);
		b = FF(b, c, d, a, x[11], 22, 0x895CD7BE);
		//
		a = FF(a, b, c, d, x[12], 7, 0x6B901122);
		d = FF(d, a, b, c, x[13], 12, 0xFD987193);
		c = FF(c, d, a, b, x[14], 17, 0xA679438E);
		b = FF(b, c, d, a, x[15], 22, 0x49B40821);
		//
		// Round 2
		a = GG(a, b, c, d, x[1], 5, 0xF61E2562);
		d = GG(d, a, b, c, x[6], 9, 0xC040B340);
		c = GG(c, d, a, b, x[11], 14, 0x265E5A51);
		b = GG(b, c, d, a, x[0], 20, 0xE9B6C7AA);
		//
		a = GG(a, b, c, d, x[5], 5, 0xD62F105D);
		d = GG(d, a, b, c, x[10], 9, 0x2441453);
		c = GG(c, d, a, b, x[15], 14, 0xD8A1E681);
		b = GG(b, c, d, a, x[4], 20, 0xE7D3FBC8);
		//
		a = GG(a, b, c, d, x[9], 5, 0x21E1CDE6);
		d = GG(d, a, b, c, x[14], 9, 0xC33707D6);
		c = GG(c, d, a, b, x[3], 14, 0xF4D50D87);
		b = GG(b, c, d, a, x[8], 20, 0x455A14ED);
		//
		a = GG(a, b, c, d, x[13], 5, 0xA9E3E905);
		d = GG(d, a, b, c, x[2], 9, 0xFCEFA3F8);
		c = GG(c, d, a, b, x[7], 14, 0x676F02D9);
		b = GG(b, c, d, a, x[12], 20, 0x8D2A4C8A);
		//
		// Round 3
		a = HH(a, b, c, d, x[5], 4, 0xFFFA3942);
		d = HH(d, a, b, c, x[8], 11, 0x8771F681);
		c = HH(c, d, a, b, x[11], 16, 0x6D9D6122);
		b = HH(b, c, d, a, x[14], 23, 0xFDE5380C);
		//
		a = HH(a, b, c, d, x[1], 4, 0xA4BEEA44);
		d = HH(d, a, b, c, x[4], 11, 0x4BDECFA9);
		c = HH(c, d, a, b, x[7], 16, 0xF6BB4B60);
		b = HH(b, c, d, a, x[10], 23, 0xBEBFBC70);
		//
		a = HH(a, b, c, d, x[13], 4, 0x289B7EC6);
		d = HH(d, a, b, c, x[0], 11, 0xEAA127FA);
		c = HH(c, d, a, b, x[3], 16, 0xD4EF3085);
		b = HH(b, c, d, a, x[6], 23, 0x4881D05);
		//
		a = HH(a, b, c, d, x[9], 4, 0xD9D4D039);
		d = HH(d, a, b, c, x[12], 11, 0xE6DB99E5);
		c = HH(c, d, a, b, x[15], 16, 0x1FA27CF8);
		b = HH(b, c, d, a, x[2], 23, 0xC4AC5665);
		//
		// Round 4
		a = II(a, b, c, d, x[0], 6, 0xF4292244);
		d = II(d, a, b, c, x[7], 10, 0x432AFF97);
		c = II(c, d, a, b, x[14], 15, 0xAB9423A7);
		b = II(b, c, d, a, x[5], 21, 0xFC93A039);
		//
		a = II(a, b, c, d, x[12], 6, 0x655B59C3);
		d = II(d, a, b, c, x[3], 10, 0x8F0CCC92);
		c = II(c, d, a, b, x[10], 15, 0xFFEFF47D);
		b = II(b, c, d, a, x[1], 21, 0x85845DD1);
		//
		a = II(a, b, c, d, x[8], 6, 0x6FA87E4F);
		d = II(d, a, b, c, x[15], 10, 0xFE2CE6E0);
		c = II(c, d, a, b, x[6], 15, 0xA3014314);
		b = II(b, c, d, a, x[13], 21, 0x4E0811A1);
		//
		a = II(a, b, c, d, x[4], 6, 0xF7537E82);
		d = II(d, a, b, c, x[11], 10, 0xBD3AF235);
		c = II(c, d, a, b, x[2], 15, 0x2AD7D2BB);
		b = II(b, c, d, a, x[9], 21, 0xEB86D391);
		//
		//
		x[16] += a;
		x[17] += b;
		x[18] += c;
		x[19] += d;
	}
}